package so.ifl.manager.admin;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.except.SessionTimeOutException;
import so.ifl.servcie.ConsultService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.ConsultVo;

/**
 * 咨询信息manager
 * create by simon
 * create at 2011-12-29 上午11:32:43
 * <mailto:liangyuanliu@gmail.com>
 */
@Component("consultManager")
public class ConsultManager {
	@Autowired
	private ConsultService consultService;
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());

	/**
	 * 咨询信息分页查询
	 * simon
	 * create at 2011-12-29 上午11:33:47
	 * <mailto:liangyuanliu@gmail.com>
	 * @param page
	 * @param map
	 */
	@DataProvider
	public void getConsult(Page<ConsultVo> page, Map<String, Object> map) {
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (map == null) {
			map = new HashMap<String, Object>();
		}
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			map.put("company_id", accountVo.getCompanyId());
		}
		
		List<ConsultVo> list = consultService.queryConsultWithParamAndPagination(map, offset, limmit);
		int countsql = consultService.countConsultWithParam(map);
		page.setEntities(list);
		page.setEntityCount(countsql);
	}
	/**
	 * 回仿信息分页查询
	 * simon
	 * create at 2012-1-16 上午11:57:57
	 * <mailto:liangyuanliu@gmail.com>
	 * @param page
	 * @param map
	 */
	@DataProvider
	public void getReturnVisit(Page<ConsultVo> page, Map<String, Object> map) {
		int offset = page.getFirstEntityIndex();
		int limit = page.getPageSize();
		if (map == null) {
			map = new HashMap<String, Object>();
		}
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			map.put("company_id", accountVo.getCompanyId());
		}
		List<ConsultVo> list = consultService.queryConsultWithTimeToRemind(map, offset, limit);
		int countsql = consultService.countConsultWithTimeToRemind(map);
		page.setEntities(list);
		page.setEntityCount(countsql);
	}

	/**
	 * 咨询信息curd
	 * simon
	 * create at 2011-12-29 上午11:33:20
	 * <mailto:liangyuanliu@gmail.com>
	 * @param consults
	 */
	@DataResolver
	public void updateConsult(Collection<ConsultVo> consults) {
		for (ConsultVo consult : consults) {
			EntityState entityState = EntityUtils.getState(consult);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.info("新增");
				insertConsult(consult);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.info("修改");
				updateConsult(consult);
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.info("删除");
				deleteConsult(consult.getConsultId());
			}
		}
	}

	/**
	 * 新增咨询信息
	 * simon
	 * create at 2011-12-29 上午11:25:38
	 * <mailto:liangyuanliu@gmail.com>
	 * @param consult
	 */
	private void insertConsult(ConsultVo consult) {
		Map<String, Object> m = utilMap(consult);
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			m.put("company_id", accountVo.getCompanyId());
			m.put("consult_status_", 0);//默认状态为0，1为确认报培。
			m.put("create_user_", accountVo.getAccountOwner());
			m.put("create_time_", new Date());
		}else{
			throw new SessionTimeOutException("登录超时。。");
		}		
		consultService.insertConsult(m);
	}
	/**
	 * 删除咨询信息
	 * simon
	 * create at 2011-12-29 下午3:46:05
	 * <mailto:liangyuanliu@gmail.com>
	 * @param consultId
	 */
	private void deleteConsult(Integer consultId)
	{
		consultService.deleteConsult(consultId);		
	}
	
	/**
	 * 修改咨询信息
	 * simon
	 * create at 2011-12-29 下午5:40:08
	 * <mailto:liangyuanliu@gmail.com>
	 * @param consult
	 */
	private void updateConsult(ConsultVo consult)
	{
		Map<String, Object> m = utilMap(consult);
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			// 更新时，要从session里面取值
			m.put("consult_id_", consult.getConsultId());
			m.put("last_modify_user", accountVo.getAccountOwner());
			m.put("last_modify_time_", new Date());
			m.put("consult_status_", consult.getConsultStatus());		
		}else{
			throw new SessionTimeOutException("登录超时。。");
		}		
		consultService.updateConsult(m);
	}

	/**
	 * simon create at 2011-12-29 上午11:24:53
	 * <mailto:liangyuanliu@gmail.com>
	 * @param consult
	 * @param add
	 * @return
	 */
	private Map<String, Object> utilMap(ConsultVo consult) {
		Map<String, Object> m = new HashMap<String, Object>();			
		m.put("consult_name_", consult.getConsultName());
		m.put("consult_phone_", consult.getConsultPhone());
		m.put("consult_register_time_", consult.getConsultRegisterTime());
		m.put("consult_note_", consult.getConsultNote());
		return m;
	}

}
